<?php
class item_addtopost extends template_item
{
	var $default_name = "Add to post count";
	var $default_description = "Add {post} to your post count";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item['item_name'] = str_replace('{post}',$this->item_config['add_post'],$this->item['item_name']);
			
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['add_post'] = 200;
		}
		
		$this->description = str_replace('{post}',$this->item_config['add_post'],$this->item['item_description']);
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{post} = The amount of posts count to be added<br />It is settable below.",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>How many posts to add to the post count?</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][add_post]", $this->item_config['add_post']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['add_post'] = intval($input['add_post']);
		
		$item_info['item_config'] = serialize($save);
		
		$item_info['item_description'] = str_replace("#post#", $save['add_post'], $item_info['item_description']);
		
		if($this->ipsclass->DB->field_exists("posts_added", "members"))
		{
			$this->ipsclass->DB->sql_change_field("members","posts_added","posts_added","int NOT NULL","'0'");
		}
		else
		{
			$this->ipsclass->DB->sql_add_field("members","posts_added","int NOT NULL","'0'");
			
		}
	}
	
	function admin_remove()
	{
		if(count($this->ipsclass->market_class->item_by_folder[$this->item['filename']]) == 1)
		{
			if(!isset($this->ipsclass->input['restore_posts']))
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																			 	  	 1 => array( 'section'   , 'components'      ),
																					 2 => array( 'act'       , 'market'          ),
																					 3 => array( 'code'      , 'item'            ),
																					 4 => array( 'do'        , 'delete_item'     ),
																					 5 => array( 'item'      , $this->ipsclass->input['item']),
																			)		);
				
				$this->ipsclass->adskin->td_header[] = array( "" , "" );
				$this->ipsclass->adskin->td_header[] = array( "" , "" );
				
				$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Deletion Info");
				
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																					"<b>Do you want to restore the post count to the original state?</b>",
																					$this->ipsclass->adskin->form_yes_no('restore_posts', 1),
																			)	);
				
				$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Remove Item");
				
				$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
				
				$this->ipsclass->admin->output();
			}
			else
			{
				if($this->ipsclass->input['restore_posts'])
				{
					$this->ipsclass->DB->build_and_exec_query(array('update'=>'members','set'=>"posts=posts-posts_added"));
				}
				$this->ipsclass->DB->sql_drop_field("members","posts_added");
			}
		}
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$amount = $this->ipsclass->numbers->multiply(count($inventory_ids),$this->item_config['add_post']);
		
		$this->ipsclass->DB->do_update('members',array(
														'posts' => $this->ipsclass->numbers->add($this->ipsclass->member['posts'],$amount),
														'posts_added' => $this->ipsclass->numbers->add($this->ipsclass->member['posts_added'],$amount)
				),"id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->lang['add_x_post'] = str_replace('<#AMOUNT#>', $amount, $this->ipsclass->lang['add_x_post']);
		
		$this->delete_items($inventory_ids);
		
		foreach($inventory_ids as $inv_id)
		{
			$this->_write_log($inv_id, "Added ".$this->item_config['add_post']." to post count");
		}
		
		$this->ipsclass->market_class->redirect('add_x_post');
	}
	
	function parse_store_description()
	{
		return $this->description;
	}
	
	function parse_group_description()
	{
		return $this->description;
	}
	
	function parse_single_description()
	{
		return $this->description;
	}
}
?>